package com.lyf.emall.controller;

import com.lyf.emall.bean.Page;
import com.lyf.emall.bean.ResponseResult;
import com.lyf.emall.bean.SysUser;
import com.lyf.emall.service.UserService;
import com.lyf.emall.utils.FileUploadUtil;
import com.lyf.emall.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Timestamp;

/**
 * @Description:user控制器
 * @Author Lfy
 * @Date 2022/2/17-15:37
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    FileUploadUtil fileUploadUtil;

    @GetMapping("/user/repeat")
    @PreAuthorize("hasAuthority('/user/repeat')")
    public ResponseResult isSameUserName(@RequestParam("userName") String userName){
        return userService.isSameUserName(userName);
    }


    @PostMapping("/user")
    @PreAuthorize("hasAuthority('/user/add')")
    public ResponseResult addUser(String userName,
                                  String password,
                                  String phoneNumber,
                                  String email,
                                  String sex,
                                  String address,
                                  String description,
                                  MultipartFile userFile,
                                  @RequestHeader String token) throws Exception {
        // 解析token
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();
        // 判断是否要文件上传
        String upload = null;
        if(userFile != null){
            // 文件上传
            upload = fileUploadUtil.upload(userFile);   // 上传的文件名
        }

        // 密码加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);  // 加密后的密码

        // 时间戳
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SysUser user = new SysUser(null, userName, encode, phoneNumber, email, sex, address, 1, 1, 0, upload, description, timestamp, timestamp, Integer.valueOf(userId), 3);

        return userService.addUser(user);
    }

    @DeleteMapping("/user")
    @PreAuthorize("hasAuthority('/user/delete')")
    public ResponseResult deleteUser(@RequestParam("userId") String userId){
        return userService.deleteUser(Integer.valueOf(userId));
    }

    @PostMapping("/user/update")
    @PreAuthorize("hasAuthority('/user/update')")
    public ResponseResult stopUser(@RequestParam("userId") String userId,
                                   @RequestParam("status") String status){
        Integer s = 0;
        if("1".equals(status)){
            s = 0;
        }else if("0".equals(status)){
            s = 1;
        }
        return userService.stopUser(Integer.valueOf(userId), s);
    }

    @PostMapping("/user/get")
    @PreAuthorize("hasAuthority('/user/get')")
    public ResponseResult getUserInfo(@RequestParam("userId") String userId){
        return userService.getUserInfo(Integer.valueOf(userId));
    }

    @PutMapping("/user")
    @PreAuthorize("hasAuthority('/user/put')")
    public ResponseResult updateUserInfo(String userId,
                                         String userName,
                                         String phoneNumber,
                                         String email,
                                         String sex,
                                         String address,
                                         MultipartFile userFile,
                                         String description,
                                         @RequestHeader String token) throws Exception {
        // 解析token
        Claims claims = JwtUtil.parseJWT(token);
        String updateId = claims.getSubject();

        // 判断是否要文件上传
        String upload = null;
        if(userFile != null){
            // 文件上传
            upload = fileUploadUtil.upload(userFile);   // 上传的文件名
        }

        // 时间戳
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SysUser user = new SysUser(Integer.valueOf(userId), userName, null, phoneNumber, email, sex, address, null, null, null, upload, description, null, timestamp, Integer.valueOf(updateId), null);

        ResponseResult responseResult = userService.updateUserInfo(user);
        return responseResult;
    }

    @PutMapping("/user/psw")
    @PreAuthorize("hasAuthority('/user/psw')")
    public ResponseResult updatePassword(@RequestParam String userId,
                                         @RequestParam String password,
                                         @RequestParam String psw,
                                         @RequestHeader String token) throws Exception {
        if (!password.equals(psw)){
            return new ResponseResult(400, "两次密码不一致");
        }

        // 解析token
        Claims claims = JwtUtil.parseJWT(token);
        String updateId = claims.getSubject();

        // 时间戳
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(psw);

        SysUser user
                = new SysUser(Integer.valueOf(userId), null, encode, null, null, null, null, null, null, null, null, null, null, timestamp, Integer.valueOf(updateId), null);
        return userService.updatePassword(user);
    }

    @GetMapping("/user")
    @PreAuthorize("hasAuthority('/user/list')")
    public ResponseResult getSysUser(Page page){
        return userService.getAllUserByPage(page);
    }

    @GetMapping("/user/query")
    @PreAuthorize("hasAuthority('/user/query')")
    public ResponseResult getSysUserByUserName(@RequestParam String userName,
                                               Page page){
        return userService.getSysUserByUserName(userName, page);
    }

    @GetMapping("/user/delete")
    @PreAuthorize("hasAuthority('/user/delete')")
    public ResponseResult getDeleteSysUser(Page page){
        return userService.getAllDeleteUser(page);
    }

    @PostMapping("/user/restore")
    @PreAuthorize("hasAuthority('/user/restore')")
    public ResponseResult restore(@RequestParam String userId){
        return userService.restore(Integer.valueOf(userId));
    }

    @GetMapping("/user/delete/list")
    @PreAuthorize("hasAuthority('/user/delete/list')")
    public ResponseResult getDeleteUserByUserName(@RequestParam String userName,
                                                  Page page){
        return userService.getDeleteUserByUserName(userName, page);
    }
}
